<?php

class Control_NewsController extends Zend_Controller_Action {

    public function init() {
        $this->_helper->layout->setLayout('admin');
        $this->yesButton = 'Yes'; //напис на кнопці Так під час видалення
        $this->noButton = 'No'; //напис на кнопці Ні під час видалення
        $this->newsDb = new Control_Model_Table_News();
        $this->view->controller = $this->getRequest()->getControllerName();
        $this->view->action = $this->getRequest()->getActionName();
    }

    public function indexAction() {
        $this->_helper->redirector('edit');
    }

    public function addAction() {
       $form = new Control_Model_Form_News();
        $form->submit->setLabel('Add');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $title = $form->getValue('title');
                $long_text = $form->getValue('long_text');
                $link = $form->getValue('link');
                $date = $form->getValue('date');
                $main = $form->getValue('main');
                $this->newsDb->addNews($title, $long_text, $link, $date, $main);
                $this->_helper->redirector('edit');
            } else {
                $form->populate($formData);
            }
        }
    }

    public function editAction() {
        $this->view->PortfolioData = $this->newsDb->getAllNews();
        $form = new Control_Model_Form_News();
        $form->submit->setLabel('Update');
        $this->view->form = $form;
        $delparam = $this->_getParam('del', 0);
        ///---------------------------------------------------------------------//
        if ($this->getRequest()->isPost() && ($delparam != 1)) { //Якщо відправлена форма без видалення
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int) $form->getValue('id');
                $title = $form->getValue('title');
                $long_text = $form->getValue('long_text');
                $link = $form->getValue('link');
                $date = $form->getValue('date');
                $main = $form->getValue('main');
                $this->newsDb->updateNews($id, $title, $long_text, $link, $date, $main);
                $this->_helper->redirector('edit');
            } else {
                $form->populate($formData);
            }
        } elseif ($this->getRequest()->isPost() && ($delparam == 1)) { //Якщо відправлена форма з видаленням
            if ($this->getRequest()->isPost()) {
                $del = $this->getRequest()->getPost('del');
                if ($del == $this->yesButton) {
                    $id = $this->getRequest()->getPost('id');
                    $this->newsDb->deleteData($id);
                } else {
                    $this->_helper->redirector('edit');
                }
                $this->_helper->redirector('edit');
            }
        } else {//Виведення сторінки едіт
            $id = (int) $this->_getParam('id', 0);
            $del = (int) $this->_getParam('del', 0);
            $this->view->get_id = $id;
            if ($id > 0 && $del == 0) {
                $formdata = $this->newsDb->getDataID($id);
                if (isset($formdata)) {
                    $form->populate($formdata);
                } else {
                    $this->view->get_id = 0;
                };
            } elseif ($id > 0 && $del == 1) {
                $this->view->get_del = 1;
                $this->view->yesButton = $this->yesButton;
                $this->view->noButton = $this->noButton;
                $this->view->NameToDelete = $this->newsDb->getDataID($id);
            } elseif ($id == 0) {
                $this->view->get_id = $id;
            } else {
                $this->view->get_id = 0;
                echo "<span style='color: red;'>Error id: $id<br></span>";
            }
        }
    }

}

